package com.bjsxt.datastructure;

import java.util.Arrays;
import java.util.EmptyStackException;

public class MySatck<E> {

    private Object[] arr;
    private int stackLength =4;
    private int size;
    private int index=-1;

    boolean empty(){

        return this.size==0;
    }

    public E pop(){
        //如果为空，抛出异常
        if (this.index==-1){
            throw new EmptyStackException();
        }
        this.size--;
        return (E) this.arr[index--];
    }

    public E push(E item){
        this.capacity();
        this.arr[++index]=item;
        this.size++;
        return item;
    }

    private void capacity(){
        //初始化
        if (this.arr==null){
            this.arr=new Object[this.stackLength];
        }
        //扩容
        if (this.size-(this.stackLength-1)>=0){
            this.stackLength = this.stackLength+(this.stackLength>>1);
            this.arr= Arrays.copyOf(this.arr,this.stackLength );
        }
    }

    public static void main(String[] args){
        MySatck<String> mySatck = new MySatck<>();
        mySatck.push("a");
        mySatck.push("e");
        mySatck.push("e");
        mySatck.push("e");
        mySatck.push("e");
        mySatck.pop();

    }

}
